package icu.makka.yygh.cmn.controller;

import icu.makka.yygh.cmn.service.DictService;
import icu.makka.yygh.cmn.service.impl.DictServiceImpl;
import icu.makka.yygh.model.cmn.Dict;
import icu.makka.yygh.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictServiceImpl dictService;

    /**
     * 根据dictCode获取下级节点
     * @param dictCode
     * @return
     */
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable("dictCode") String dictCode) {
        log.info("根据dictCode获取下级节点");
        List<Dict> dictList = dictService.findByDictCode(dictCode);
        return Result.ok(dictList);
    }

    /**
     * 根据id查询子数据列表
     * @param id
     * @return
     */
    @ApiOperation("根据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        log.info("根据id查询子数据列表，id={}", id);
        //调用业务层
        List<Dict> childData = dictService.findChildData(id);
        //返回结果
        return Result.ok(childData);
    }

    /**
     * 根据dictcode和value查询数据字典
     * @param dictCode
     * @param value
     * @return
     */
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable("dictCode") String dictCode,
                          @PathVariable("value") String value) {
        log.info("根据dictcode和value查询数据字典，dictcode={}，value={}", dictCode, value);

        //调用service层
        String dictName = dictService.getDictName(dictCode, value);

        //返回结果
        return dictName;
    }

    /**
     * 根据value查询数据字典
     * @param value
     * @return
     */
    @GetMapping("getName/{value}")
    public String getName(@PathVariable("value") String value) {
        log.info("根据value查询数据字典，value={}", value);

        //调用service层
        String dictName = dictService.getDictName(null, value);

        //返回结果
        return dictName;
    }
}
